<template>
  <div>
    <FilterTable
      ref="table"
      columnsRef="reportPartSales_part"
      :columns="columns"
      selectionType="single"
      :onColumnsChange="columns => (this.columns = columns)"
      :onRowSelectChange="arr => (this.selectColumns = arr)"
      :isExportExcel="true"
      exportFileName="配件综合情况查询-配件销售.xlsx"
      :dataSource="dataSource"
      :isMemoryPagination="true"
    >
      <!-- :fetchapi="fetchApi"
      :params="fetchParams"
      :datakey="'rows'"-->
      <template slot="controls">
        <AjaxButton size="small" type="primary" :disabled="printFlag" icon="el-icon-printer" :click="printHandle">打印</AjaxButton>
      </template>
      <template slot="controls">
        <AjaxButton size="small" type="primary" icon="el-icon-printer" :click="exportHandle">导出</AjaxButton>
      </template>
    </FilterTable>
    <!-- 打印 -->
    <div v-if="showPrint">
      <BasePrint ref="Salesprint" :data="printList" template="templateSales" />
    </div>
  </div>
</template>
<script>
import { dictionary } from '@/utils/dictMixin';
import dictCode from '@/utils/dictCode';
import { queryPartInOutStockReport, getAllUser, exportPartInOutStockReport } from '@/api/part/report/billSearch';
import { queryCompanyInforById, queryTex } from '@/api/part/common';
import { getCompanyId } from '@/assets/js/auth';
import { getStroageList } from '@/api/part/partOutbound/partBreak';
import { getModel } from '@/api/customerRepair/customerCare/insuranceDueRemind/index';
import { getWxlxSelect } from '@/api/repair/repairReception/customer';
import { exportExcelFile } from '@/utils/index';
import { searchRoleByCode } from '@/api/common/empSelect/index';
import { printData } from '@/api/part/partOut/partSales/index';
import { doSearch } from '@/api/tool/basic/toolParams';
import { dealerPrintApi } from '@/api/repair/index.js';
export default {
  mixins: [dictionary],
  props: {
    fetchParamObj: Object,
    type: String,
    userList: Array
  },

  data() {
    return {
      fetchApi: queryPartInOutStockReport,
      columns: this.createdTable(),
      fetchParams: { xhrAbort: true },
      selectColumns: [], // 选中行数据
      showPrint: true, // 打印显示
      printList: {}, // 打印数据
      printParams: {},
      printFlag: false, // 打印按钮是否禁用
      dataSource: []
    };
  },
  watch: {
    fetchParamObj(fetchParamObj) {
      console.log(fetchParamObj, 'fetchParamObj----');
      this.initTableData(fetchParamObj);
    }
  },
  mounted() {
    this.getStorage();
    this.queryModel();
    this.getWxtypeSelect();
    this.serviceConsultant();
    this.getEmployee();
    // this.getPrintParams();
  },
  methods: {
    // 打印数据接口
    async getPrintData() {
      // console.log(this.selectColumns[0].PART_ALLOCATE_IN_ID);
      const res = await printData(this.selectColumns[0].SALES_PART_NO); // 获取当前行数据
      let explainArr = [];
      this.printList = res.data;
      // 获取公司信息
      let componyInfo = '';
      // try {
      //   componyInfo = await queryCompanyInforById({
      //     owenrCode: getCompanyId()
      //   });
      // } catch (err) {
      //   console.log('获取公司信息失败', err);
      // }
      // 查询配件销售单说明
      // try {
      //   let explainRes = await doSearch({ module: 'spa' });
      //   let explainKeyArr = ['XSDSM', 'XSDSM1', 'XSDSM2'];
      //   // "XSDSM" "XSDSM1" "XSDSM2" bParaValue
      //   if (explainRes.resultCode == 200) {
      //     explainKeyArr.map(item => {
      //       let itemStr = explainRes.data.find(item2 => item2.para == item) ? explainRes.data.find(item2 => item2.para == item).bParaValue : '';
      //       itemStr && explainArr.push(itemStr);
      //     });
      //   }
      //   console.log(explainArr, '配件系统参数，说明部分。。。。。');
      // } catch (err) {
      //   console.log(err, '获取配件销售单说明失败');
      // }
      this.printList.compInfo = componyInfo.data;
      this.printList.explainArr = explainArr || [];
    },
    async getPrintParams() {
      let params = { vModule: '90211009', vNormalCode: 'BJXS' };
      const res = await dealerPrintApi(params);
      if ((res.resultCode == 200) & res.data.length) {
        this.printParams = res.data[0];
      }

      console.log('打印的参数', this.printParams);
    },
    //导出
    async exportHandle(){
      let res = await exportPartInOutStockReport(this.fetchParams);
      console.log(res,"导出::");
      try{
        exportExcelFile(res.data,'配件销售')
      }catch(e){
        console.log(e,"错误::");
      }
    },
    // 打印处理
    async printHandle() {
      // this.getPrintParams();
      // 判断是否选中数据
      if (this.selectColumns.length < 1) {
        return this.$notify.error({ title: '提示信息', message: '请选中一条数据' });
      }
      await this.getPrintData();
      this.showPrint = true;
      console.log(this.printList);
      this.printList.baseInfo.USER_NAME = this.userList[this.printList.baseInfo.USER_ID];
      // 打印
      this.$nextTick(() => {
        // console.log(123);
        this.$refs.Salesprint.EXCUTE_PRINT();
      });
    },
    createdTable() {
      return [
        {
          title: '序号',
          dataIndex: 'index',
          width: 70,
          sorter: true,
          render: props => {
            return <span>{props.row.index + 1}</span>;
          }
        },
        { title: '销售单号', dataIndex: 'SALES_PART_NO', sorter: true, filter: true, filterType: 'input', showOverflowTooltip: true },
        // { title: '原单编号', dataIndex: 'fake1', sorter: true, filter: true, filterType: 'input', showOverflowTooltip: true },
        { title: '对应单编号', dataIndex: 'OLD_SALES_PART_NO', sorter: true, filter: true, filterType: 'input', showOverflowTooltip: true },
        // { title: '出库方式', dataIndex: 'fake2', sorter: true, filter: true, filterType: 'input', showOverflowTooltip: true },
        { title: '出库日期', dataIndex: 'FINISHED_DATE', sorter: true, filter: true, filterType: 'date-range', showOverflowTooltip: true, exportFormat: { t: 'd' } },
        { title: '客户名称', dataIndex: 'CUSTOMER_NAME', sorter: true, filter: true, filterType: 'input', showOverflowTooltip: true },
        // { title: '牌照号', dataIndex: 'LICENSE', sorter: true, filter: true, filterType: 'input', showOverflowTooltip: true },
        { title: '仓库代码', dataIndex: 'STORAGE_CODE', dictItems: [], editItems: [], sorter: true, filter: true, filterType: 'input', showOverflowTooltip: true },
        // { title: '税率', dataIndex: 'TAX', sorter: true, filter: true, filterType: 'input', showOverflowTooltip: true },
        { title: '操作员代码', dataIndex: 'SENDER', sorter: true, filter: true, filterType: 'input', showOverflowTooltip: true },
        {
          title: '操作员名称',
          dataIndex: 'OPERATOR_NAME',
          render: props => {
            return <span>{this.userList[props.row.OPERATOR_NAME]}</span>;
          },
          dictItems: [],
          sorter: true,
          filter: true,
          filterType: 'input',
          showOverflowTooltip: true
        },
        { title: '配件代码', dataIndex: 'PART_NO', width: '160', sorter: true, filter: true, filterType: 'input', showOverflowTooltip: true },
        // { title: '批号', dataIndex: 'fake7', sorter: true, filter: true, filterType: 'input', showOverflowTooltip: true },
        { title: '配件名称', dataIndex: 'PART_NAME', sorter: true, filter: true, filterType: 'input', showOverflowTooltip: true },
        // {
        //   title: '配件说明',
        //   dataIndex: 'PART_DESCRIPTION',
        //   sorter: true,
        //   showOverflowTooltip: true,
        //   filter: true,
        //   filterType: 'input',
        //   showOverflowTooltip: true
        // },
        // { title: 'CCC件类型', dataIndex: 'IS_CCC_PART', sorter: true, filter: true, filterType: 'input', showOverflowTooltip: true },
        // {
        //   title: ' ABC分类',
        //   dataIndex: 'ABC_TYPE',
        //   sorter: true,
        //   filter: true,
        //   filterType: 'checkbox',
        //   filterItems: this.createDictList('8092'),
        //   dictItems: this.createDictList('8092'),
        //   render: props => {
        //     return <span>{this.createDictText(props.row.ABC_TYPE, '8092')}</span>;
        //   },
        //   showOverflowTooltip: true
        // },
        // { title: '计量单位', dataIndex: 'UNIT_NAME', sorter: true, filter: true, filterType: 'input', showOverflowTooltip: true },
        { title: '出库数量', dataIndex: 'PART_QUANTITY', summation: true, sorter: true, filter: true, filterType: 'number', showOverflowTooltip: true },
        { title: '含税出库单价', dataIndex: 'PART_SALES_PRICE', sorter: true, filter: true, filterType: 'number', precision: 2, summation: false, showOverflowTooltip: true },
        { title: '含税成本单价', dataIndex: 'PART_COST_PRICE', sorter: true, filter: true, filterType: 'number', precision: 2, showOverflowTooltip: true },
        { title: '含税出库金额', dataIndex: 'PART_SALES_AMOUNT', summation: true, sorter: true, filter: true, filterType: 'number', precision: 2, showOverflowTooltip: true },
        { title: '含税成本金额', dataIndex: 'PART_COST_AMOUNT', summation: true, sorter: true, filter: true, filterType: 'number', precision: 2, showOverflowTooltip: true }
        // { title: '主组号', dataIndex: 'MAIN_GROUP_NO', sorter: true, filter: true, filterType: 'input', showOverflowTooltip: true },
        // {
        //   title: '销售方式',
        //   dataIndex: 'SALES_TYPE',
        //   render: props => {
        //     return <span>{this.createDictText(props.row.SALES_TYPE, '8096')}</span>;
        //   },
        //   sorter: true,
        //   filter: true,
        //   filterType: 'checkbox',
        //   filterItems: this.createDictList('8096'),
        //   dictItems: this.createDictList('8096'),
        //   showOverflowTooltip: true
        // },
        // { title: '发布单号', dataIndex: 'fake8', sorter: true, filter: true, filterType: 'input', showOverflowTooltip: true },
        // { title: '备注', dataIndex: 'REMARK', sorter: true, filter: true, filterType: 'input', showOverflowTooltip: true }
        // {
        //   title: '配件销售单号',
        //   dataIndex: 'SALES_PART_NO',
        //   sorter: true
        // },
        // {
        //   title: '配件成本',
        //   dataIndex: 'COST_PRICE',
        //   sorter: true
        // },
        // // {
        // //   title: '车牌号',
        // //   dataIndex: 'LICENSE',
        // //   sorter: true
        // // },
        // {
        //   title: '车主编号',
        //   dataIndex: 'CUSTOMER_CODE',
        //   sorter: true
        // },
        // {
        //   title: '车主',
        //   dataIndex: 'CUSTOMER_NAME',
        //   sorter: true
        // },
        // {
        //   title: '结算时间',
        //   dataIndex: 'BALANCE_TIME',
        //   sorter: true,
        //   width: '160'
        // },
        // {
        //   title: '工单号',
        //   dataIndex: 'RO_NO',
        //   sorter: true
        // },
        // // {
        // //   title: '客户名称',
        // //   dataIndex: 'CUSTOMER_NAME',
        // //   sorter: true,
        // //   showOverflowTooltip: true
        // // },
        // {
        //   title: '销售材料费',
        //   dataIndex: 'PART_SALES_AMOUNT',
        //   sorter: true
        // },
        // // {
        // //   title: '维修类型',
        // //   dataIndex: 'REPAIR_TYPE_CODE',
        // //   sorter: true,
        // //   editable: false,
        // //   editType: 'select',
        // //   editItems: []
        // // },
        // // {
        // //   title: '车型',
        // //   dataIndex: 'MODEL',
        // //   sorter: true,
        // //   editable: false,
        // //   editType: 'select',
        // //   editItems: []
        // // },
        // // {
        // //   title: '工单类型',
        // //   dataIndex: 'RO_TYPE',
        // //   sorter: true,
        // //   render: props => {
        // //     return <span>{this.createDictText(props.row.RO_TYPE, '8068')}</span>;
        // //   }
        // // },
        // {
        //   title: '服务专员',
        //   dataIndex: 'CONSULTANT',
        //   sorter: true,
        //   editable: false,
        //   editType: 'select',
        //   editItems: []
        // },
        // {
        //   title: '仓库',
        //   dataIndex: 'STORAGE_CODE',
        //   sorter: true,
        //   editable: false,
        //   editType: 'select',
        //   editItems: []
        // }
      ];
    },

    async initTableData(obj) {
      const fetchParamObj = this.fetchParamObj;
      // this.fetchParams = Object.assign({}, fetchParamObj, { xhrAbort: false });
      this.fetchParams = Object.assign({}, fetchParamObj);
      try {
        this.$refs.table.START_LOADING();
        this.dataSource = [];
        let res = await queryPartInOutStockReport(this.fetchParams);
        this.dataSource = res.data;
        this.$refs.table.STOP_LOADING();
      } catch (error) {
        console.log(error);
      } finally {
        this.$refs.table.STOP_LOADING();
      }
    },

    // 获取仓库
    async getStorage() {
      const res = await getStroageList();
      this.columns.find(x => x.dataIndex === 'STORAGE_CODE').dictItems = this.columns.find(x => x.dataIndex === 'STORAGE_CODE').editItems = res.data.map(x => ({
        text: x.STORAGE_NAME,
        value: x.STORAGE_CODE + ''
      }));
    },

    // 查询所有车型(供table使用)
    async queryModel() {
      const res = await getModel();
      // this.columns.find(x => x.dataIndex === 'MODEL').editItems = res.data.map(x => ({ text: x.MODEL_NAME, value: x.MODEL_ID + '' }));
    },

    // 获取维修项目类型
    async getWxtypeSelect() {
      const res = await getWxlxSelect();
      // this.columns.find(x => x.dataIndex === 'REPAIR_TYPE_CODE').editItems = res.data.map(x => ({
      //   value: x.REPAIR_TYPE_CODE + '',
      //   text: x.REPAIR_TYPE_NAME
      // }));
    },

    // 售前人员接口数据
    async serviceConsultant() {
      let consultant = { role: dictCode.SERVICE_CONSULTANT }; // 服务专员
      // const consultantRes = await searchRoleByCode(consultant);
      // this.columns.find(x => x.dataIndex === 'SERVICE_ADVISOR').editItems = consultantRes.data.map(x => ({ text: x.employeeName, value: x.userId + '' }));
    },

    // 操作员名称
    async getEmployee() {
      // 获取人员列表
      const res = await getAllUser();
      if (res.resultCode == 200) {
        this.columns.find(item => item.dataIndex === 'OPERATOR_NAME').dictItems = res.data.map(x => ({
          text: x.employeeName,
          value: x.userId + ''
        }));
      }
    }
  }
};
</script>

<style lang="less" scoped>
/* .form-group {
  margin-top: 10px;
  span {
    height: 50px;
    line-height: 50px;
    text-align: right;
    width: 250px;
    padding-left: 100px;
  }
} */
</style>
